﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OLMS.DAL;

namespace OLMS.BLL
{
    public class AlbumBLL
    {
        //获取全部音乐专辑
        public static List<Albums> GetAlbumsList()
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Albums.Include("Artists").ToList();
            }
        }
        //根据专辑编号获取专辑信息
        public static Albums GetAlbumsById(int albumsId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Albums.Include("Artists").SingleOrDefault(a=>a.AlbumId==albumsId);
            }
        }
        //添加和编辑音乐专辑
        public static bool AddOrEditAlbum(Albums albums)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                if (albums.AlbumId == 0)
                {
                    db.Albums.Add(albums);
                }
                else
                {
                    db.Albums.Attach(albums);                    
                }
                return db.SaveChanges()!=-1?true:false;
            }           
        }
        //删除音乐专辑
        public static bool RemoveAlbum(int albumId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Albums albums = db.Albums.SingleOrDefault(a => a.AlbumId == albumId);
                if (albums != null)
                {
                    db.Albums.Remove(albums);
                    return db.SaveChanges() != -1 ? true : false;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}